/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sys.service;

import java.util.List;

import com.jeesite.common.idgen.IdGen;

import com.jeesite.common.utils.UuidUtil;
import com.jeesite.modules.sys.dao.OfficeDao;
import com.jeesite.modules.sys.entity.Office;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.poifs.property.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.service.TreeService;
import com.jeesite.modules.sys.dao.CompanyDao;
import com.jeesite.modules.sys.dao.CompanyOfficeDao;
import com.jeesite.modules.sys.entity.Company;
import com.jeesite.modules.sys.entity.CompanyOffice;
import com.jeesite.modules.sys.utils.EmpUtils;

/**
 * 公司管理Service
 * @author thinkgem
 * @version 2016-4-23
 */
@Service
@Transactional(readOnly=true)
public class CompanyService extends TreeService<CompanyDao, Company> {

	@Autowired
	private CompanyOfficeDao companyOfficeDao;

	@Autowired
	private OfficeService officeService;

	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private DataScopeService dataScopeService;
	
	/**
	 * 获取单条数据
	 */
	@Override
	public Company get(Company company) {
		return super.get(company);
	}
	
	/**
	 * 添加数据权限过滤条件
	 */
	@Override
	public void addDataScopeFilter(Company company, String ctrlPermi) {

		company.getSqlMap().getDataScope().addFilter("dsf", "Company", "a.company_code", ctrlPermi);
	}

	/**
	 * 查询公司列表
	 */
	@Override
	public List<Company> findList(Company company) {
		return super.findList(company);
	}

	/**
	 * 保存公司
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Company company) {
		if (company.getIsNewRecord()){
			genIdAndValid(company, company.getViewCode());
			company.setParentCode("0");

			if(company.getParent()==null) {
				Company parent = new Company();
				parent.setId("0");
				company.setParent(parent);
			}
			dataScopeService.insertIfParentExists(company, "Company");
		}
		super.save(company);
		if(company.getIsNewRecord()){
			if(company.getParentCodes().equals("0,")) {
				Office office = new Office();
				if(office.getParent()==null) {
					Office parent = new Office();
					parent.setId("0");
					office.setParent(parent);
				}

				office.setIsNewRecord(true);
				office.setTreeSort(30);
				office.setOfficeName(company.getCompanyName());
				office.setLeader(company.getHead());
				office.setContact(company.getContact());
				office.setAddress(company.getAddress());
				office.setEmail(company.getEmail());
				office.setCompanyCode(company.getCompanyCode());
				office.setParentCode("0");

				String uid = UuidUtil.get32UUID();
				office.setOfficeCode(uid);
				office.setViewCode(uid);

				officeService.save(office);

				// 当前新数据授权，如果用户有上级数据权限，则当前数据也有相应的数据权限
			}

		}

		// 重新绑定组织和公司之间的关系
		if (StringUtils.isBlank(company.getCompanyCode())){
			return;
		}
		CompanyOffice where = new CompanyOffice();
		where.setCompanyCode(company.getCompanyCode());
		companyOfficeDao.deleteByEntity(where);
		if (ListUtils.isNotEmpty(company.getCompanyOfficeList())){
			companyOfficeDao.insertBatch(company.getCompanyOfficeList());
		}

		// 清理公司相关缓存
		clearCompanyCache();
	}
	
	/**
	 * 删除公司
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Company company) {
		super.delete(company);
		// 清理公司相关缓存
		clearCompanyCache();
	}

	/**
	 * 停用当前节点
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Company company) {
		dao.updateStatus(company);
		// 清理公司相关缓存
		clearCompanyCache();
	}
	
	/**
	 * 清理公司相关缓存
	 */
	private void clearCompanyCache(){
//		EmpUtils.removeCache(EmpUtils.CACHE_COMPANY_LIST);
		EmpUtils.removeCache(EmpUtils.CACHE_COMPANY_ALL_LIST);
	}

}